﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameControls;
using Microsoft.Xna.Framework.Graphics;
using ThouShaltNotBrick.Logic.Helpers;

namespace ThouShaltNotBrick.Logic.GameStates
{
    internal sealed class PlayStartGameState : PlayGameStateBase
    {
        #region Constants
        private const float INITIAL_SECONDS_TO_START = 3.0f;
        #endregion

        #region Properties
        private Label mCountdownLabel;

        private float mSecondsToStart;
        #endregion

        #region Events
        #endregion

        #region Constructors
        #endregion

        #region Overriden Methods
        protected override void InitializeImpl()
        {
            base.InitializeImpl();

            mIsMouseVisible = false;

            InitializeControls();
        }

        protected override void DestroyImpl()
        {
            DestroyControls();

            base.DestroyImpl();
        }

        protected override void StartImpl(GameStateChangeData pChangeData)
        {
            SetPlayData(pChangeData);
            mPlayData.LoadCurrentLevel();
            mSecondsToStart = INITIAL_SECONDS_TO_START;
        }

        protected override void StopImpl()
        {
        }

        protected override void ResumeImpl(GameStateChangeData pChangeData)
        {
            SetPlayData(pChangeData);
        }

        protected override void PauseImpl()
        {
        }

        protected override void HandleInputImpl(GameTime pGameTime)
        {
        }

        protected override void UpdateImpl(GameTime pGameTime)
        {
            if (mSecondsToStart > 0.0)
            {
                mSecondsToStart -= (float)pGameTime.ElapsedGameTime.TotalSeconds;
            }
            else
            {
                ChangeToAnotherPlayState(GameStateType.PlayNormal);
            }

            mCountdownLabel.Text = Math.Ceiling(mSecondsToStart).ToString("0");
        }

        protected override void DrawImpl(GameTime pGameTime)
        {
            mLevelDrawer.Draw(pGameTime, mPlayData);
        }
        #endregion

        #region Methods
        private void InitializeControls()
        {
            DestroyControls();

            int screenWidth = mGameManager.GetClientWidth();
            int screenHeight = mGameManager.GetClientHeight();

            Rectangle shadeImageRect = new Rectangle(0, 0, screenWidth, screenHeight);
            Image shadeImage = new Image(shadeImageRect, mGraphicsResources.ShadeTexture);
            mControls.Add(shadeImage);

            SpriteFont startingGameFont = mFontResources.Font20;
            int statingGameLabelHeight = (int)startingGameFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle startingGameLabelRect = new Rectangle(0, screenHeight / 2 - statingGameLabelHeight, screenWidth, statingGameLabelHeight);
            Label startingGameLabel = new Label(startingGameLabelRect);
            startingGameLabel.SetText("Starting game in...", startingGameFont, Color.Red);
            startingGameLabel.HorizontalAlignment = HorizontalAlignment.Center;
            startingGameLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(startingGameLabel);

            SpriteFont countdownFont = mFontResources.Font20;
            int countdownLabelHeight = (int)countdownFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle countdownLabelRect = new Rectangle(0, screenHeight / 2, screenWidth, countdownLabelHeight);
            mCountdownLabel = new Label(countdownLabelRect);
            mCountdownLabel.SetText(string.Empty, countdownFont, Color.Red);
            mCountdownLabel.HorizontalAlignment = HorizontalAlignment.Center;
            mCountdownLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(mCountdownLabel);
        }

        private void DestroyControls()
        {
            mControls.Clear();
        }
        #endregion

        #region Event Handlers
        #endregion

        #region Inner Classes
        #endregion
    }
}